using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using CMS.Application.Common.Interface;
using CMS.Application.RequestDto;
using CMS.Domain.Entity;
using Microsoft.AspNetCore.Mvc;

namespace CMS.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UserController : ControllerBase
    {

        private readonly IRepository<Users> _Users;
        private readonly IIdentityService _IdentityService;
        private readonly IRepository<Register> _register;
        private readonly IRepository<AttentionAuthor> _attentionAuthor;


        public UserController(IRepository<Users> Users, IIdentityService IdentityService, IRepository<Register> register, IRepository<AttentionAuthor> attentionAuthor)
        {
            _Users = Users;
            _IdentityService = IdentityService;
            _register = register;
            _attentionAuthor = attentionAuthor;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="changePwdDto"></param>
        /// <returns></returns>
        [HttpPost("/ChangePwd")]
        public async Task<object> ChangePwd(ChangePwdDto changePwdDto)
        {
            var IsUser = _Users.Table.FirstOrDefault(x => x.Email == changePwdDto.Email);
            if (IsUser == null)
            {
                return new
                {
                    code = "400",
                    msg = "该用户不存在,请注册!!!",
                    data = ""
                };
            }
            else
            {
                if (changePwdDto.PassWord == "")
                {
                    return new
                    {
                        code = "400",
                        msg = "密码不可为空",
                        data = ""
                    };
                }
                if (changePwdDto.PassWord != changePwdDto.AgainPassword)
                {
                    return new
                    {
                        code = "400",
                        msg = "密码不一致",
                        data = ""
                    };
                }
                if (IsUser.PassWord == changePwdDto.PassWord)
                {
                    return new
                    {
                        code = 400,
                        msg = "密码不可与原密码一致",
                        data = ""
                    };
                }
                if (changePwdDto.PassWord.Length < 6)
                {
                    return new
                    {

                        code = "400",

                        data = "",

                        msg = "密码长度不可小于6"


                    };
                }
                // 判断密码是否存在中文
                var IsCN = Regex.IsMatch(changePwdDto.PassWord, @"[\u4e00-\u9fa5]");

                if (IsCN)
                {

                    return new
                    {

                        code = "400",

                        data = "",

                        msg = "密码中不可存在中文！"


                    };

                }


                // 判断邮箱和验证码是否正确
                var Code = _register.Table.FirstOrDefault(

                    x => x.Email == changePwdDto.Email && x.AuthCode == changePwdDto.Code

       );

                var Newdate = DateTime.UtcNow;

                if (Code == null)
                {


                    return new
                    {

                        code = "400",

                        data = "",

                        msg = "验证码错误！"

                    }
                    ;

                }


                if ((Newdate - Code.UpdatedAt).TotalMinutes >= 5)
                {
                    // 验证码过期删除数据
                    await _register.DeleteAsync(Code.Id, true);

                    return new
                    {

                        code = "400",

                        data = "",

                        msg = "验证码已过期"

                    };
                }
                var user = await _Users.GetByIdAsync(IsUser.Id);
                user.PassWord = changePwdDto.PassWord;
                await _Users.UpdateAsync(user);

                return new
                {
                    code = 200,
                    msg = "修改成功",
                    data = "",
                };
            }
        }

        //修改密码邮箱验证码发送
        [HttpPost("/APwdEmail")]
        public async Task<object> Email(EmailDto emailDto)
        {
            // 判断邮箱是否已经被注册
            var IsEmail = _Users.Table.FirstOrDefault(x => x.Email == emailDto.Email);

            if (IsEmail == null)
            {

                return new
                {

                    code = "400",

                    data = "",

                    msg = "该邮箱不存在,请注册！！"

                };

            }


            // 判断是否是使用了QQ邮箱
            var IsQQ = Regex.IsMatch(emailDto.Email, "@qq.com");

            if (!IsQQ)
            {
                return new
                {

                    code = "400",

                    data = "",

                    msg = "目前仅支持QQ邮箱哦"

                };
            }

            // 调用封装后的发送邮件的静态类
            var EmailServer = new EmailServser(_register, _Users, emailDto.Email);

            var IsEmailServer = await EmailServer.ToEmail();

            return IsEmailServer;

        }

        //删除用户
        [HttpDelete("/delete/{id}")]
        public async Task<object> deleteUser(Guid id)
        {
            
            var orgin = await _Users.GetByIdAsync(id);
            if (orgin != null)
            {
                await _Users.DeleteAsync(id, true);
                var u = await _Users.GetByIdAsync(id);
                if (u == null)
                {
                    return new
                    {
                        code = "200",
                        msg = "删除成功",
                        data = ""
                    };
                }
                else
                {
                    return new
                    {
                        code = "400",
                        msg = "删除成功",
                        data = ""
                    };
                }
            }
            else
            {
                return new
                {
                    code = "400",
                    msg = "无此用户",
                    data = ""
                };
            }
        }

        /// <summary>
        /// 关注用户
        /// 
        /// </summary>
        /// <param name="attentionAuthorDto"></param>
        /// <returns></returns>

        [HttpPost("/AttentionAuthor")]
        public async Task<object> AttentionAuthor(AttentionAuthorDto attentionAuthorDto)
        {
            var UserId = await _Users.GetByIdAsync(attentionAuthorDto.UserId);
            var AuthorId = await _Users.GetByIdAsync(attentionAuthorDto.AuthorId);

            if (UserId == null)
            {
                return new
                {
                    code = "400",
                    msg = "请重新登录",
                    data = ""
                };
            }
            else if (AuthorId == null)
            {
                return new
                {
                    code = "400",
                    msg = "不存在该用户，或该用户以注销",
                    data = ""
                };
            }
            else
            {
                var entity = new AttentionAuthor
                {
                    UserId = attentionAuthorDto.UserId,
                    AuthorId = attentionAuthorDto.AuthorId
                };
                var present = _attentionAuthor.Table.Where(x => x.UserId == attentionAuthorDto.UserId && x.AuthorId == attentionAuthorDto.AuthorId).ToList();
                if (present.Count!=0)
                {
                    return new
                    {
                        code = 200,
                        msg = "以关注该用户",
                        data = ""
                    };
                }else
                {
                    await _attentionAuthor.AddAsync(entity);
                    return new
                    {
                        code = 200,
                        msg = "关注成功",
                        data = ""
                    };
                }

            }
        }
    }
}